<!-- 表单页面示例 -->
<template>
  <div class="p-6">
    <!-- 页面标题 -->
    <div class="flex items-center mb-6">
      <el-button @click="$router.back()" :icon="ArrowLeft" text>返回</el-button>
      <h1 class="text-2xl font-bold ml-2">{{ isEdit ? '编辑用户' : '新增用户' }}</h1>
    </div>

    <!-- 表单卡片 -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border p-6">
      <el-form 
        ref="formRef"
        :model="userForm" 
        :rules="formRules"
        label-width="100px"
        class="max-w-2xl"
      >
        <!-- 基础信息 -->
        <div class="mb-6">
          <h3 class="text-lg font-semibold mb-4 flex items-center">
            <el-icon class="mr-2"><User /></el-icon>
            基础信息
          </h3>
          
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userForm.username" placeholder="请输入用户名" />
          </el-form-item>
          
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userForm.email" placeholder="请输入邮箱" />
          </el-form-item>
          
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userForm.phone" placeholder="请输入手机号" />
          </el-form-item>
          
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="userForm.gender">
              <el-radio label="1">男</el-radio>
              <el-radio label="2">女</el-radio>
              <el-radio label="0">未知</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>

        <!-- 权限设置 -->
        <div class="mb-6">
          <h3 class="text-lg font-semibold mb-4 flex items-center">
            <el-icon class="mr-2"><Lock /></el-icon>
            权限设置
          </h3>
          
          <el-form-item label="角色" prop="roleId">
            <el-select v-model="userForm.roleId" placeholder="请选择角色">
              <el-option label="管理员" value="1" />
              <el-option label="编辑员" value="2" />
              <el-option label="观察员" value="3" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="部门" prop="departmentId">
            <el-cascader
              v-model="userForm.departmentId"
              :options="departmentOptions"
              placeholder="请选择部门"
              clearable
            />
          </el-form-item>
          
          <el-form-item label="权限" prop="permissions">
            <el-checkbox-group v-model="userForm.permissions">
              <el-checkbox label="user:read">用户查看</el-checkbox>
              <el-checkbox label="user:write">用户编辑</el-checkbox>
              <el-checkbox label="user:delete">用户删除</el-checkbox>
              <el-checkbox label="system:admin">系统管理</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </div>

        <!-- 状态设置 -->
        <div class="mb-6">
          <h3 class="text-lg font-semibold mb-4 flex items-center">
            <el-icon class="mr-2"><Setting /></el-icon>
            状态设置
          </h3>
          
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="userForm.status"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="userForm.remark"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
              maxlength="200"
              show-word-limit
            />
          </el-form-item>
        </div>
      </el-form>
    </div>

    <!-- 操作按钮 -->
    <div class="flex justify-center gap-4 mt-6">
      <!-- primary 按钮自动跟随主题色 -->
      <el-button type="primary" @click="handleSubmit" :loading="loading">
        {{ isEdit ? '更新' : '创建' }}
      </el-button>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="info" @click="handleReset">重置</el-button>
    </div>

    <!-- 操作记录（如果是编辑模式） -->
    <div v-if="isEdit" class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border p-6 mt-6">
      <h3 class="text-lg font-semibold mb-4">操作记录</h3>
      <el-timeline>
        <el-timeline-item timestamp="2024-01-15 10:30" type="primary">
          用户创建成功
        </el-timeline-item>
        <el-timeline-item timestamp="2024-01-16 14:20" type="success">
          更新用户信息
        </el-timeline-item>
        <el-timeline-item timestamp="2024-01-17 09:15" type="warning">
          修改用户权限
        </el-timeline-item>
      </el-timeline>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowLeft, User, Lock, Setting } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

// 🎯 无需导入主题功能，Element Plus 组件自动跟随主题
// 如果需要主题切换功能，只需要：
// const { setThemeColor } = useTheme()

// 表单数据
const formRef = ref(null)
const loading = ref(false)
const isEdit = computed(() => route.params.id)

const userForm = ref({
  username: '',
  email: '',
  phone: '',
  gender: '1',
  roleId: '',
  departmentId: [],
  permissions: [],
  status: 1,
  remark: ''
})

// 表单验证规则
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  roleId: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

// 部门选项
const departmentOptions = ref([
  {
    value: '1',
    label: '技术部',
    children: [
      { value: '1-1', label: '前端组' },
      { value: '1-2', label: '后端组' },
      { value: '1-3', label: '测试组' }
    ]
  },
  {
    value: '2',
    label: '产品部',
    children: [
      { value: '2-1', label: '产品组' },
      { value: '2-2', label: '设计组' }
    ]
  }
])

// 表单方法
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    const valid = await formRef.value.validate()
    if (!valid) return
    
    loading.value = true
    
    // 模拟提交
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    ElMessage.success(isEdit.value ? '用户更新成功' : '用户创建成功')
    router.back()
  } catch (error) {
    ElMessage.error('操作失败')
  } finally {
    loading.value = false
  }
}

const handleCancel = () => {
  router.back()
}

const handleReset = async () => {
  try {
    await ElMessageBox.confirm('确定要重置表单吗？', '提示', {
      type: 'warning'
    })
    
    formRef.value?.resetFields()
    ElMessage.success('表单已重置')
  } catch {
    // 用户取消
  }
}
</script>
